package com.rj.core.collections;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据处理List
 *
 * @author YFX_RANLUBO
 * @date 2022/10/20 17:11
 **/
public class DataHandlerList<T> extends ArrayList<T> {
    protected static final ThreadLocal<Map<String, DataHandlerListMonitor>> LOCAL = new ThreadLocal();
    /**
     * 设置监听器
     * @param monitor
     * @param key
     */
    public static void addMonitor(String  key, DataHandlerListMonitor monitor) {
        Map<String, DataHandlerListMonitor> monitorMap = LOCAL.get();
        if (monitorMap == null) {
            HashMap<String, DataHandlerListMonitor> monitors = new HashMap<>(3);
            monitors.put(key, monitor);
            LOCAL.set(monitors);
            return;
        }
        monitorMap.put(key, monitor);
    }

     @Override
    public boolean add(T t) {
            Map<String, DataHandlerListMonitor> monitorMap = LOCAL.get();
            if (monitorMap != null) {
                DataHandlerListMonitor monitor = monitorMap.get(t.toString());
                if (monitor != null) {
                    monitor.handler(t);
                }
            }
        return super.add(t);
    }
}
